package longrise.phone.com.bjjt_henan.main;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.FormParameter;
import com.longrise.android.ILSMsgListener;
import com.longrise.android.LFView;
import com.longrise.bjjt.util.UiUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.SocializeUtils;

import java.util.ArrayList;
import java.util.List;

import longrise.phone.com.bjjt_henan.Adapter.ShareAdapter;
import longrise.phone.com.bjjt_henan.Constant.MsgConstant;
import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.PermissionHelper;
import longrise.phone.com.bjjt_henan.Utils.RecycleBitmap;

/**
 * 推荐好友界面
 */
public class RecommendView extends LFView implements ILSMsgListener {

    private static final String LOAD = "http://220.231.252.128/hnkckp_webmap/hnkckp_ConTraDownLoad";

    private Context context;

    private View view;

    private ImageView imageView;

    private GridView gridView;

    private List<EntityBean> shareLists;

    public RecommendView(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter fp = new FormParameter();
        fp.setShowtitle(false);
        return fp;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        if (null == context) {
            return;
        }
        view = LayoutInflater.from(context).inflate(R.layout.view_recommend, null);
        if (null == view) {
            return;
        }
        //标题栏
        TextView titleTV = (TextView) view.findViewById(R.id.tv_title);
        titleTV.setText("推荐好友");
        view.findViewById(R.id.hn_btn_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeForm();
                OnDestroy();
            }
        });
        imageView = (ImageView) view.findViewById(R.id.recommend_iv_code);
        gridView = (GridView) view.findViewById(R.id.recommend_gv);

        showShareView();
        this.addILSMsgListener(this);
    }

    //显示分享列表
    private void showShareView() {
        getShareData();
        ShareAdapter adapter = new ShareAdapter(context);
        gridView.setAdapter(adapter);
        adapter.setDataLists(shareLists);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!PermissionHelper.checkPermissions(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    return;
                }
                EntityBean bean = shareLists.get(position);
                switch (bean.getInt("id")) {
                    case 0://新浪微博
                        shareTo(SHARE_MEDIA.SINA);
                        break;
                    case 1://微信朋友圈
                        shareTo(SHARE_MEDIA.WEIXIN_CIRCLE);
                        break;
                    case 2://微信
                        shareTo(SHARE_MEDIA.WEIXIN);
                        break;
                    case 3://QQ空间
                        shareTo(SHARE_MEDIA.QZONE);
                        break;
                    case 4://QQ好友
                        shareTo(SHARE_MEDIA.QQ);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    //分享到其他应用
    private void shareTo(SHARE_MEDIA var1) {
        ShareAction action = new ShareAction((Activity) context);
        action.setPlatform(var1);

        UMWeb web = new UMWeb(LOAD);
        web.setTitle("交通事故快处快赔");//标题
        web.setDescription("轻微交通事故手机处理，交警定责、保险报案、定责理赔全程最快5分钟！河南交警、保监会联合出品");//描述
        web.setThumb(new UMImage(context, R.mipmap.logo09));  //缩略图

        action.withMedia(web);//附带的图片，音乐，视频等多媒体对象
        action.setCallback(umShareListener);//设置友盟分享后状态监听
        action.share();//发起分享，调起微信，QQ，微博客户端进行分享
    }

    private ProgressDialog umDialog;

    private UMShareListener umShareListener = new UMShareListener() {

        @Override
        public void onStart(SHARE_MEDIA platform) {
            if (umDialog == null) {
                umDialog = new ProgressDialog(context);
            }
            SocializeUtils.safeShowDialog(umDialog);
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            if (platform.name().equals("WEIXIN_FAVORITE")) {
                UiUtil.showToast(context, "收藏成功");
            } else {
                UiUtil.showToast(context, "分享成功");
            }
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            SocializeUtils.safeCloseDialog(umDialog);
            UiUtil.showToast(context, "分享失败");
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
//            UiUtil.showToast(context, platform + " 分享取消啦");
        }
    };

    @Override
    public Object onLSMsg(int i, Object... objects) {
        if (i == MsgConstant.RESUME) {
            SocializeUtils.safeCloseDialog(umDialog);
        }
        return null;
    }

    @Override
    public void refreshByTime() {

    }

    @Override
    public void refresh() {

    }

    //获取分享的类型
    private void getShareData() {
        if (null == shareLists) {
            shareLists = new ArrayList<EntityBean>();
        } else {
            shareLists.clear();
        }
//        EntityBean sinaBean = new EntityBean();
//        sinaBean.set("id", 0);
//        sinaBean.set("text", "新浪微博");
//        sinaBean.set("resid", R.mipmap.ico01);
//        shareLists.add(sinaBean);
//        sinaBean = null;

        EntityBean wxBean = new EntityBean();
        wxBean.set("id", 2);
        wxBean.set("text", "微信");
        wxBean.set("resid", R.mipmap.ico03);
        shareLists.add(wxBean);
        wxBean = null;

        EntityBean wxfBean = new EntityBean();
        wxfBean.set("id", 1);
        wxfBean.set("text", "微信朋友圈");
        wxfBean.set("resid", R.mipmap.ico02);
        shareLists.add(wxfBean);
        wxfBean = null;

        EntityBean qqBean = new EntityBean();
        qqBean.set("id", 4);
        qqBean.set("text", "QQ好友");
        qqBean.set("resid", R.mipmap.ico05);
        shareLists.add(qqBean);
        qqBean = null;

        EntityBean qqsBean = new EntityBean();
        qqsBean.set("id", 3);
        qqsBean.set("text", "QQ空间");
        qqsBean.set("resid", R.mipmap.ico04);
        shareLists.add(qqsBean);
        qqsBean = null;
    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        RecycleBitmap.recycleImageView(imageView);
        if (null != shareLists) {
            shareLists.clear();
            shareLists = null;
        }
        if (null != gridView) {
            gridView.setAdapter(null);
            gridView.setOnItemClickListener(null);
            gridView = null;
        }
        view = null;
        umShareListener = null;
        removeILSMsgListener(this);
    }
}
